package com.example.pre.controller;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.pre.controller.base.ControllerBase;
import com.example.pre.enums.ReimbursementTypeEnum;
import com.example.pre.exception.ExceptionMessage;
import com.example.pre.exception.ExtParaException;
import com.example.pre.exception.ServiceException;
import com.example.pre.tools.Tools;

/*
 * 报销controller层接口
 */
@RestController
public class ReimbursementController extends ControllerBase {

	private final Logger logger = LoggerFactory.getLogger(UserController.class);

	/**
	 * 创建报销申请
	 * 
	 * @param type            报销类型
	 * @param description     报销事由
	 * @param billingDataList 报销明细json集合
	 * @return
	 */
	@RequestMapping(value = "/reimbursement", method = RequestMethod.POST)
	public Map<String, Object> createReimbursement(@RequestParam ReimbursementTypeEnum type,
			@RequestParam(required = false) String description, @RequestParam Set<String> billingDataList) {
		// 获取响应时间
		long start = System.currentTimeMillis();
		String eventId = Tools.createUUID();
		String message = "";
		String inputPara = ",传入的参数type=" + type + "description=" + description + "billingDataList=" + billingDataList;
		// 预创建返回值
		HashMap<String, Object> result = new HashMap<String, Object>();
		result.put("flag", false);
		result.put("message", "OK");
		result.put("code", 0);
		result.put("data", null);
		Integer userId = this.getUserId();
		try {
			// 参数验证
			if (CollectionUtils.isEmpty(billingDataList)) {
				result.put("message", "参数有误");
				result.put("code", -501);
				message = "报销明细集合不能为空";
				logger.error(super.createLog(new Exception(), message + inputPara, start, eventId));
				return result;
			}

			// 存返回数据
			message = "创建报销申请申请成功";
			result.put("flag", true);
			result.put("message", message);
			logger.info(super.createLog(message + inputPara, start, eventId));
			return result;
		} catch (ServiceException e) {
			ExceptionMessage exceptionMessage = e.getExceptionMessage();
			int code = exceptionMessage.getCode();
			result.put("code", code);
			result.put("message", exceptionMessage.getShowMessage());
			message = "创建报销申请时，业务报错：" + exceptionMessage.getLogMessage();
			logger.error(super.createLog(e, message + inputPara, start, eventId));
			return result;
		} catch (ExtParaException e) {
			ExceptionMessage exceptionMessage = e.getExceptionMessage();
			int code = exceptionMessage.getCode();
			result.put("code", code);
			result.put("message", exceptionMessage.getShowMessage());
			message = "创建报销申请时，第三方业务报错：" + exceptionMessage.getLogMessage();
			logger.error(super.createLog(e, message + inputPara, start, eventId));
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", -400);
			result.put("message", "系统繁忙，请稍后重试");
			message = "创建报销申请时，系统报错：" + e.getMessage();
			logger.error(super.createLog(e, message + inputPara, start, eventId));
			return result;
		}
	}

	/**
	 * 修改报销申请
	 * 
	 * @param reimbursementId 报销申请id
	 * @param type            报销类型
	 * @param description     报销描述
	 * @param billingDataList 报销明细json集合
	 * @return
	 */
	@RequestMapping(value = "/reimbursement", method = RequestMethod.PUT)
	public Map<String, Object> updateReimbursement(@RequestParam Integer reimbursementId,
			@RequestParam(required = false) ReimbursementTypeEnum type,
			@RequestParam(required = false) String description,
			@RequestParam(required = false) Set<String> billingDataList) {
		// 获取响应时间
		long start = System.currentTimeMillis();
		String eventId = Tools.createUUID();
		String message = "";
		String inputPara = ",传入的参数,reimbursementId=" + reimbursementId + "type=" + type + "description=" + description
				+ "billingDataList=" + billingDataList;
		// 预创建返回值
		HashMap<String, Object> result = new HashMap<String, Object>();
		result.put("flag", false);
		result.put("message", "OK");
		result.put("code", 0);
		result.put("data", null);
		Integer userId = this.getUserId();
		try {
			// 参数验证
			if (reimbursementId == null || reimbursementId < 0) {
				result.put("message", "参数有误");
				result.put("code", -501);
				message = "报销id不能为空或小于0";
				logger.error(super.createLog(new Exception(), message + inputPara, start, eventId));
				return result;
			}

			// 存返回数据
			message = "修改报销申请申请成功";
			result.put("flag", true);
			result.put("message", message);
			logger.info(super.createLog(message + inputPara, start, eventId));
			return result;
		} catch (ServiceException e) {
			ExceptionMessage exceptionMessage = e.getExceptionMessage();
			int code = exceptionMessage.getCode();
			result.put("code", code);
			result.put("message", exceptionMessage.getShowMessage());
			message = "修改报销申请时，业务报错：" + exceptionMessage.getLogMessage();
			logger.error(super.createLog(e, message + inputPara, start, eventId));
			return result;
		} catch (ExtParaException e) {
			ExceptionMessage exceptionMessage = e.getExceptionMessage();
			int code = exceptionMessage.getCode();
			result.put("code", code);
			result.put("message", exceptionMessage.getShowMessage());
			message = "修改报销申请时，第三方业务报错：" + exceptionMessage.getLogMessage();
			logger.error(super.createLog(e, message + inputPara, start, eventId));
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", -400);
			result.put("message", "系统繁忙，请稍后重试");
			message = "修改报销申请时，系统报错：" + e.getMessage();
			logger.error(super.createLog(e, message + inputPara, start, eventId));
			return result;
		}
	}

	/**
	 * 查看报销申请详情
	 * 
	 * @param reimbursementId 报销申请id
	 * @return
	 */
	@RequestMapping(value = "/reimbursement", method = RequestMethod.GET)
	public Map<String, Object> checkReimbursement(@RequestParam Integer reimbursementId) {
		// 获取响应时间
		long start = System.currentTimeMillis();
		String eventId = Tools.createUUID();
		String message = "";
		String inputPara = ",传入的参数,reimbursementId=" + reimbursementId;
		// 预创建返回值
		HashMap<String, Object> result = new HashMap<String, Object>();
		result.put("flag", false);
		result.put("message", "OK");
		result.put("code", 0);
		result.put("data", null);
		try {
			// 参数验证
			if (reimbursementId == null || reimbursementId < 0) {
				result.put("message", "参数有误");
				result.put("code", -501);
				message = "报销id不能为空或小于0";
				logger.error(super.createLog(new Exception(), message + inputPara, start, eventId));
				return result;
			}

			// 存返回数据
			message = "查看报销申请申请成功";
			result.put("flag", true);
			result.put("message", message);
			logger.info(super.createLog(message + inputPara, start, eventId));
			return result;
		} catch (ServiceException e) {
			ExceptionMessage exceptionMessage = e.getExceptionMessage();
			int code = exceptionMessage.getCode();
			result.put("code", code);
			result.put("message", exceptionMessage.getShowMessage());
			message = "查看报销申请时，业务报错：" + exceptionMessage.getLogMessage();
			logger.error(super.createLog(e, message + inputPara, start, eventId));
			return result;
		} catch (ExtParaException e) {
			ExceptionMessage exceptionMessage = e.getExceptionMessage();
			int code = exceptionMessage.getCode();
			result.put("code", code);
			result.put("message", exceptionMessage.getShowMessage());
			message = "查看报销申请时，第三方业务报错：" + exceptionMessage.getLogMessage();
			logger.error(super.createLog(e, message + inputPara, start, eventId));
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", -400);
			result.put("message", "系统繁忙，请稍后重试");
			message = "查看报销申请时，系统报错：" + e.getMessage();
			logger.error(super.createLog(e, message + inputPara, start, eventId));
			return result;
		}
	}

	/**
	 * 发起报销申请
	 * 
	 * @param reimbursementId 报销申请id
	 * @return
	 */
	@RequestMapping(value = "/reimbursement/flow", method = RequestMethod.POST)
	public Map<String, Object> flowReimbursement(@RequestParam Integer reimbursementId) {
		// 获取响应时间
		long start = System.currentTimeMillis();
		String eventId = Tools.createUUID();
		String message = "";
		String inputPara = ",传入的参数,reimbursementId=" + reimbursementId;
		// 预创建返回值
		HashMap<String, Object> result = new HashMap<String, Object>();
		result.put("flag", false);
		result.put("message", "OK");
		result.put("code", 0);
		result.put("data", null);
		try {
			// 参数验证
			if (reimbursementId == null || reimbursementId < 0) {
				result.put("message", "参数有误");
				result.put("code", -501);
				message = "报销id不能为空或小于0";
				logger.error(super.createLog(new Exception(), message + inputPara, start, eventId));
				return result;
			}

			// 存返回数据
			message = "发起报销申请申请成功";
			result.put("flag", true);
			result.put("message", message);
			logger.info(super.createLog(message + inputPara, start, eventId));
			return result;
		} catch (ServiceException e) {
			ExceptionMessage exceptionMessage = e.getExceptionMessage();
			int code = exceptionMessage.getCode();
			result.put("code", code);
			result.put("message", exceptionMessage.getShowMessage());
			message = "发起报销申请时，业务报错：" + exceptionMessage.getLogMessage();
			logger.error(super.createLog(e, message + inputPara, start, eventId));
			return result;
		} catch (ExtParaException e) {
			ExceptionMessage exceptionMessage = e.getExceptionMessage();
			int code = exceptionMessage.getCode();
			result.put("code", code);
			result.put("message", exceptionMessage.getShowMessage());
			message = "发起报销申请时，第三方业务报错：" + exceptionMessage.getLogMessage();
			logger.error(super.createLog(e, message + inputPara, start, eventId));
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", -400);
			result.put("message", "系统繁忙，请稍后重试");
			message = "发起报销申请时，系统报错：" + e.getMessage();
			logger.error(super.createLog(e, message + inputPara, start, eventId));
			return result;
		}
	}

}
